package com.maiyuren.fishkit.ui.components

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActionScope
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import com.maiyuren.fishkit.utils.FishFonts

@RequiresApi(Build.VERSION_CODES.Q)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun FishSearchTextField(
    value: MutableState<String>,
    onValueChange: ((String) -> Unit)? = null,
    onSearch: (KeyboardActionScope.()->Unit)? = null,
    modifier: Modifier = Modifier,
) {
//    val focusRequester = remember {
//        FocusRequester()
//    }
    BasicTextField(
        modifier = modifier,
        value = value.value,
        singleLine = true,
        maxLines = 1,
        minLines = 1,
        textStyle = TextStyle(fontFamily = FishFonts.DictFont),
        onValueChange = {
            value.value = it
            if (onValueChange != null) onValueChange(it)
        },
        keyboardActions = KeyboardActions(onSearch = onSearch),
        keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
        decorationBox = { innerTextField ->
            Box {
                Surface(
                    // border = BorderStroke(1.dp, Color.Gray),
                    color = Color.White,
                    shape = RoundedCornerShape(8.dp)
                ) {
                    Row(
                        modifier = Modifier.padding(8.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Icon(imageVector = Icons.Outlined.Search, contentDescription = null, tint = Color(0x88000000))

                        Box(modifier = Modifier
                            .padding(start = 4.dp, end = 4.dp)
                            .weight(1F)
//                            .focusRequester(focusRequester)
                        ) {
                            if (value.value.isEmpty()) Text(text = "搜尋", color = Color(0x88000000))
                            innerTextField()
                        }

//                        if (value.value.isNotEmpty()) {
//                            Icon(
//                                modifier = Modifier.clickable {
//                                    value.value = ""
//                                    focusRequester.requestFocus()
//                                },
//                                imageVector = Icons.Outlined.Close,
//                                contentDescription = null,
//                                tint = Color(0x88000000))
//                        }
//                        else null
                    }
                }
            }
        }
    )
}